//
//  HomeViewController.m
//  Mahjong
//
//  Created by mc on 16/8/29.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "HomeViewController.h"
#import "SDCycleScrollView.h"
#import "ButtonAndLabels.h"
#import "HomeCollectionViewCell.h"
#import "HomeDetailViewController.h"
#import "RepairViewController.h" //维修
#import "HomeDiyViewController.h"
#import "GoodsListViewController.h"
#import "HomeShowViewController.h"
#import "CXSearchController.h"
#import "GoodsDetailViewController.h"
#import "InformationViewController.h"
#import "AppTools.h"
#import "HomeTJModel.h"
#import "ListShowModel.h"
static NSString *identifierForGradeCell = @"identifierForGradeCell";
static NSString *headerIndentifier = @"headercollection";

@interface HomeViewController ()<UISearchBarDelegate,SDCycleScrollViewDelegate,ButtonAndLabelsDelegate,UICollectionViewDataSource,UICollectionViewDelegate>

{
    //轮播图
    SDCycleScrollView *cycleScrollView;
}

@property (nonatomic, strong) UICollectionView *goodsCollectionView;
@property (nonatomic, strong) UICollectionReusableView *headerview;
@property (nonatomic, strong) NSMutableArray *TJArray;
@property (nonatomic, strong) UIView *backWhite;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

//        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        //设置状态栏颜色
    
//        UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
//        if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
//            statusBar.backgroundColor = [UIColor blackColor];
//        }
    
    self.view.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    self.navigationController.navigationBarHidden = YES;
    
    
    
    [self create];
    [self addButtomCollectionView];
}
// 处理导航栏
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
-(void)addNav{
    self.backWhite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 66)];
    self.backWhite.backgroundColor = [UIColor colorWithRed:47/255.0 green:52/255.0 blue:53/255.0 alpha:1];
    [self.view addSubview:self.backWhite];
    self.backWhite.alpha = 0;
    
    UIButton *serviceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [serviceBtn setImage:[UIImage imageNamed:@"nav_but_notify"] forState:UIControlStateNormal];
    serviceBtn.backgroundColor = [UIColor clearColor];
    serviceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [serviceBtn addTarget:self action:@selector(serviceBtn) forControlEvents:UIControlEventTouchUpInside];
    serviceBtn.frame = CGRectMake(30, 30, 25, 25);
    [self.view addSubview:serviceBtn];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width / 4, 30, [UIScreen mainScreen].bounds.size.width / 2 + 20, 30)];
    lable.text = @"                麻将机";
    lable.font = [UIFont systemFontOfSize:15];
    lable.backgroundColor = [UIColor colorWithWhite:0.978 alpha:0.55];
    lable.textColor = [UIColor grayColor];
    lable.layer.borderColor = [UIColor colorWithWhite:0.978 alpha:0.55].CGColor;
    lable.layer.masksToBounds = YES;
    [self.view addSubview:lable];
    //    lable.layer.borderWidth = 2;
    lable.layer.cornerRadius = 5;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
    //    , 30, 20, 20]
    //    imageView.backgroundColor = [UIColor grayColor];
    [imageView setImage:[UIImage imageNamed:@"nav_icon_sousuo"]];
    [lable addSubview:imageView];
    UIButton *seachButton = [UIButton buttonWithType:UIButtonTypeSystem];
    seachButton.frame = lable.frame;
    [seachButton addTarget:self action:@selector(seachAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:seachButton];
    seachButton.backgroundColor = [UIColor clearColor];
}
-(void)serviceBtn{
    NSLog(@"serviceBtn");
    InformationViewController *info = [[InformationViewController alloc] init];
    info.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:info animated:YES];
}
-(void)seachAction{
    CXSearchController *search = [[CXSearchController alloc] init];
    search.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:search animated:YES];
 
}

#pragma mark -- 创建轮播图
-(void)addSDCycleScrollviewUI {
    // 创建不带标题的图片轮播器
    cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 4) delegate:self placeholderImage:[UIImage imageNamed:@"pic1.png"]];//最后设置占位符
    cycleScrollView.localizationImageNamesGroup = @[@"pic1.png", @"pic.png"]; //本地数组
    //网络数据
    //    cycleScrollView.imageURLStringsGroup = @[@"http://d.hiphotos.baidu.com/zhidao/pic/item/72f082025aafa40f507b2e99aa64034f78f01930.jpg", @"http://b.hiphotos.baidu.com/zhidao/pic/item/4b90f603738da9770889666fb151f8198718e3d4.jpg"];
    cycleScrollView.backgroundColor =[ UIColor clearColor];
    cycleScrollView.bannerImageViewContentMode = UIViewContentModeScaleToFill;
    cycleScrollView.showPageControl = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.headerview addSubview:cycleScrollView];
    // 轮播时间间隔，默认1.0秒，可自定义
    cycleScrollView.autoScrollTimeInterval = 4.0;
}

#pragma mark -- SDCycleScrollViewDelegate
/** 点击图片回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    //NSLog(@"点击了第%ld个图片", index);
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    //    //NSLog(@"图片滚动回调%ld", index);
}
#pragma mark -- 第二部分控件
- (void)addButtonAndLabelViews{
    NSInteger k = 0;
    NSArray *imageArray = @[@"home_but_mahjong", @"home_but_custom", @"home_but_servicing", @"home_but_promotion"];
    
    NSArray *titleArr = @[@"麻将机", @"定制", @"维修", @"促销"];
    
    for (NSInteger j=0; j<4; j++) {
        ButtonAndLabels *buttonViews = [[ButtonAndLabels alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width/4)*j, CGRectGetMaxY(cycleScrollView.frame), [UIScreen mainScreen].bounds.size.width/4, [UIScreen mainScreen].bounds.size.width/4)];
        buttonViews.delegate = self;
        buttonViews.tag = 2000+k;
        
        buttonViews.FirstLabel.text = titleArr[k];
        buttonViews.BtnImageview.image = [UIImage imageNamed:imageArray[k]];
        k++;
        [self.headerview addSubview:buttonViews];
    }
    
}
//新控件的点击事件
- (void)buttonViewbtnClick:(UIButton *)button {
//    NSLog(@"%ld",(long)button.superview.tag);
    if (2000 == button.superview.tag) {
        GoodsListViewController *repairVC = [[GoodsListViewController alloc] init];
        repairVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:repairVC animated:YES];
    }
    if (2001 == button.superview.tag) {
        HomeDiyViewController *repairVC = [[HomeDiyViewController alloc] init];
        repairVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:repairVC animated:YES];
    }
    if (2002 == button.superview.tag) {
        RepairViewController *repairVC = [[RepairViewController alloc] init];
        repairVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:repairVC animated:YES];
    }if (2003 == button.superview.tag){
        HomeShowViewController *repairVC = [[HomeShowViewController alloc] init];
        repairVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:repairVC animated:YES];
        
    }
    
}
#pragma mark -- 第三部分
-(void)addImageUI{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(cycleScrollView.frame) + 10 + [UIScreen mainScreen].bounds.size.width/4, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 9)];
    imageView.backgroundColor = [UIColor grayColor];
    [imageView setImage:[UIImage imageNamed:@"pic_zhongjian@2x"]];
    [self.headerview addSubview:imageView];
}
#pragma mark -- 第四部分
-(void)addButtonViewUI{
    UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(cycleScrollView.frame) + 20 + [UIScreen mainScreen].bounds.size.width/4 + [UIScreen mainScreen].bounds.size.height / 9, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 5)];
    backView.backgroundColor =[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    [self.headerview addSubview:backView];
    
    //销量
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width/3, backView.frame.size.height);
    leftBtn.backgroundColor = [UIColor whiteColor];
    [leftBtn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:leftBtn];
    [leftBtn setImage:[UIImage imageNamed:@"abc.jpg"] forState:UIControlStateNormal];

    UIImageView *leftBtnImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, leftBtn.frame.size.height / 5 - 10)];
    leftBtnImageView.image = [UIImage imageNamed:@"home_icon_wangguan"];
    leftBtnImageView.backgroundColor = [UIColor whiteColor];
    [leftBtn addSubview:leftBtnImageView];
    UILabel *leftBtnLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, leftBtn.frame.size.width - 40, leftBtn.frame.size.height / 5)];
    leftBtnLable.backgroundColor = [UIColor whiteColor];
    leftBtnLable.text = @"销量冠军";
    leftBtnLable.textAlignment = NSTextAlignmentCenter;
    leftBtnLable.textColor = [UIColor redColor];
    [leftBtn addSubview:leftBtnLable];
    
    
    
    // 平台
    UIButton *ptBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    ptBtn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/3 + 2, 0, [UIScreen mainScreen].bounds.size.width *2/3 - 2, backView.frame.size.height/2);
    ptBtn.backgroundColor = [UIColor whiteColor];
    [ptBtn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:ptBtn];
    UIImageView *ptImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 30, leftBtn.frame.size.height / 5 - 10)];
    ptImageView.image = [UIImage imageNamed:@"home_icon_wangguan"];
    ptImageView.backgroundColor = [UIColor whiteColor];
    [ptBtn addSubview:ptImageView];
    
    UILabel *PTBtnLable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(ptImageView.frame) + 10, 0, [UIScreen mainScreen].bounds.size.width/4, leftBtn.frame.size.height / 5)];
    PTBtnLable.backgroundColor = [UIColor whiteColor];
    PTBtnLable.text = @"平台热销";
    PTBtnLable.textColor = [UIColor redColor];
    [ptBtn addSubview:PTBtnLable];
    
    UILabel *BYlable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(PTBtnLable.frame), 0, 40, leftBtn.frame.size.height / 5)];
    BYlable.backgroundColor = [UIColor whiteColor];
    BYlable.text = @"包邮";
    BYlable.font = [UIFont systemFontOfSize:13];
    BYlable.textColor = [UIColor redColor];
    [ptBtn addSubview:BYlable];
    
    UILabel *FGlable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(ptImageView.frame)+10, CGRectGetMaxY(ptImageView.frame), [UIScreen mainScreen].bounds.size.width/4, leftBtn.frame.size.height / 4)];
    FGlable.backgroundColor = [UIColor whiteColor];
    FGlable.text = @"复古色麻将椅";
    FGlable.font = [UIFont systemFontOfSize:13];
    FGlable.textColor = [UIColor grayColor];
    [ptBtn addSubview:FGlable];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(BYlable.frame)+5, 0, backView.frame.size.height/2, backView.frame.size.height/2)];
    imgView.backgroundColor = [UIColor grayColor];
    [ptBtn addSubview:imgView];
    imgView.image = [UIImage imageNamed:@"abc.jpg"];
    UIButton *leftTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftTopBtn.frame = CGRectMake(CGRectGetMaxX(leftBtn.frame) + 2, backView.frame.size.height/2 + 2, [UIScreen mainScreen].bounds.size.width/3 - 2, backView.frame.size.height/2);
    [leftTopBtn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    leftTopBtn.backgroundColor = [UIColor whiteColor];
    [leftTopBtn setImage:[UIImage imageNamed:@"abc.jpg"] forState:UIControlStateNormal];
    [backView addSubview:leftTopBtn];
    
    UIButton *rightTopBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightTopBtn setImage:[UIImage imageNamed:@"abc.jpg"] forState:UIControlStateNormal];

    rightTopBtn.frame = CGRectMake(CGRectGetMaxX(leftTopBtn.frame) + 2, backView.frame.size.height/2 + 2, [UIScreen mainScreen].bounds.size.width/3 - 2, backView.frame.size.height/2);
    [rightTopBtn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    rightTopBtn.backgroundColor = [UIColor whiteColor];
    [backView addSubview:rightTopBtn];
}
-(void)leftButtonAction: (UIButton *)button{
    NSLog(@"leftButtonAction");
}
//
-(void)addSelectView{
    UIView *selectView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(cycleScrollView.frame) + 20 + [UIScreen mainScreen].bounds.size.width/4 + [UIScreen mainScreen].bounds.size.height * 14/ 45 , [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 18)];
    selectView.backgroundColor = [UIColor colorWithWhite:0.975 alpha:1.000];
    [self.headerview addSubview:selectView];
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/3, [UIScreen mainScreen].bounds.size.height / 72, [UIScreen mainScreen].bounds.size.height / 34, [UIScreen mainScreen].bounds.size.height / 34)];
    [imgView setImage:[UIImage imageNamed:@"icon_jingpin"]];
    //    imgView.backgroundColor = [UIColor grayColor];
    [selectView addSubview:imgView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame) + 10, 0, [UIScreen mainScreen].bounds.size.width/3, [UIScreen mainScreen].bounds.size.height / 18)];
    //    lable.backgroundColor = [UIColor whiteColor];
    lable.text = @"精选商品";
    lable.font = [UIFont systemFontOfSize:15];
    lable.textColor = [UIColor redColor];
    [selectView addSubview:lable];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - [UIScreen mainScreen].bounds.size.height / 15 - 10, 5, [UIScreen mainScreen].bounds.size.height / 15, [UIScreen mainScreen].bounds.size.height / 18 - 10);
    [btn addTarget:self action:@selector(leftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    //    btn.backgroundColor = [UIColor redColor];
    [selectView addSubview:btn];
    UIImageView *btnImg = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - [UIScreen mainScreen].bounds.size.height / 15 - 10, 10, [UIScreen mainScreen].bounds.size.height / 15, [UIScreen mainScreen].bounds.size.height / 18 - 15)];
    [btnImg setImage:[UIImage imageNamed:@"home_but_more"]];
    //    btnImg.backgroundColor = [UIColor yellowColor];
    [selectView addSubview:btnImg];
}
-(void)create{
    self.TJArray = [NSMutableArray array];
    [AppTools createAFN:@"http://api03.m0571.com/WebJson.aspx?book=GetTjList&page=1" success:^(id result) {
        
        NSLog(@"%@",[result[0] objectForKey:@"date"]);
        for (NSMutableDictionary *dic in [result[0] objectForKey:@"date"]) {
            ListShowModel *model = [[ListShowModel alloc] init];
            model.GiftMoney = [dic objectForKey:@"Giftmoney"];
            [model setValuesForKeysWithDictionary:dic];
            [self.TJArray addObject:model];
            
        }
        
        [self.goodsCollectionView reloadData];
    } error:^(NSError *error) {
        
        
    }];
}
-(void)addButtomCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //cell大小
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/2 -5, [UIScreen mainScreen].bounds.size.width/2 - 5);
    //最小的行间距
    layout.minimumLineSpacing = 10;
    //cell之间的最小距离
    layout.minimumInteritemSpacing = 10;
    
    //设置区头大小
    layout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height *2.3/3);
    //    //设置区尾大小
    //    layout.footerReferenceSize = CGSizeMake(375, 50);
    //设置分区的间距, 不包含区头和区尾, 只影响内容部分的间距
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(CGRectGetMaxY(cycleScrollView.frame), 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44) collectionViewLayout:layout];
    self.goodsCollectionView .delegate = self;
    self.goodsCollectionView .dataSource = self;
    self.goodsCollectionView .backgroundColor = [UIColor colorWithWhite:0.975 alpha:1.000];
    
    [self.view addSubview:self.goodsCollectionView ];
    [self.goodsCollectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:identifierForGradeCell];
    [self.goodsCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIndentifier];
    
}
//某个分区的CELL或者是Item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.TJArray.count;
}
//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)  collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifierForGradeCell forIndexPath:indexPath];
    ListShowModel *model = self.TJArray[indexPath.item];
//    NSLog(@"%@%@",model.Giftname,model.GiftMoney);
    cell.firstLabel.text = model.Giftname;
    cell.priceLable.text = [NSString stringWithFormat:@"¥%@",model.GiftMoney];
    cell.salesLable.text = [NSString stringWithFormat:@"销量:%@", model.salecount];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
//分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
//设置区头和区尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    self.headerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIndentifier forIndexPath:indexPath];
    //    _headerview.backgroundColor = [UIColor cyanColor];
    
    [self addSDCycleScrollviewUI];
    [self addButtonAndLabelViews];
    [self addImageUI];
    [self addButtonViewUI];
    [self addSelectView];
    [self addNav];
    return _headerview;
    
}
#pragma mark -- UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsDetailViewController *homeDetailVC = [[GoodsDetailViewController alloc] init];
    homeDetailVC.model = self.TJArray[indexPath.item];
//    homeDetailVC.shopid = model.shopid;
//    homeDetailVC.Id = model.giftdetailId;
    homeDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:homeDetailVC animated:YES];
//    NSLog(@"%@",indexPath);
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetY = scrollView.contentOffset.y;
//    NSLog(@"%.0f",offsetY);
//    CustomNavigationController *nav = (CustomNavigationController *)self.navigationController;
    if (offsetY < 130) {
        self.backWhite.alpha = offsetY/130;
    }
//    else{
//        nav.alpha = 0;
//    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBarHidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
